package me.jessyan.armscomponent.app.mvp.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import me.jessyan.armscomponent.app.R;

/**
 * Package:
 * Author: Taozebi
 * Date: 2020/9/1 10:08
 * Description: 自定义Dialog
 * Update by:
 * UpdateTime:
 */
public class CustomDialog {
    private Context context;
    private Dialog dialog;
    private LinearLayout lLayout_bg;
    private TextView txt_title;
    private TextView txt_msg;
    private Button btn_neg;
    private Button btn_pos;
    private ImageView img_line;
    private FrameLayout container;
    private View customView;
    private EditText etContent;
    private Display display;
    private boolean showTitle = false;
    private boolean showMsg = false;
    private boolean showPosBtn = false;
    private boolean showNegBtn = false;
    private OnConfirmClickListener confirmClickListener;

    public CustomDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public CustomDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_alert_dialog, null);

        // 获取自定义Dialog布局中的控件
        lLayout_bg = view.findViewById(R.id.lLayout_bg);
        txt_title = view.findViewById(R.id.txt_title);
        txt_title.setVisibility(View.GONE);
        txt_msg = view.findViewById(R.id.txt_msg);
        txt_msg.setVisibility(View.GONE);
        btn_neg = view.findViewById(R.id.btn_neg);
        btn_neg.setVisibility(View.GONE);
        btn_pos = view.findViewById(R.id.btn_pos);
        btn_pos.setVisibility(View.GONE);
        img_line = view.findViewById(R.id.img_line);
        img_line.setVisibility(View.GONE);
        container = view.findViewById(R.id.fl_content);
        etContent = view.findViewById(R.id.et_content);
        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);

        dialog.setContentView(view);

        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), LayoutParams.WRAP_CONTENT));

        return this;
    }

    /**
     * 自定义内容布局
     *
     * @param customView
     * @return
     */
    public CustomDialog setCustomView(View customView) {
        this.customView = customView;
        if (container != null) {
            container.removeAllViews();
            container.setVisibility(View.VISIBLE);
            container.addView(customView);
        }
        return this;
    }

    public CustomDialog setEditView() {
        container.setVisibility(View.GONE);
        txt_msg.setVisibility(View.GONE);
        etContent.setVisibility(View.VISIBLE);
        return this;
    }

    public CustomDialog setTitle(String title) {
        showTitle = true;
        //标题有可能为空
        /*if ("".equals(title)) {
            txt_title.setText("标题");
        } else {
            txt_title.setText(title);
        }*/
        txt_title.setText(title);
        return this;
    }

    public CustomDialog setMsg(String msg) {
        showMsg = true;
        if ("".equals(msg)) {
            txt_msg.setText("内容");
        } else {
            txt_msg.setText(msg);
        }
        return this;
    }

    public CustomDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public CustomDialog setEtContent(String content) {
        etContent.setText(content);
        return this;
    }

    public CustomDialog setInputType(int inputType) {
        etContent.setInputType(inputType);
        return this;
    }

    public CustomDialog setSingleLine() {
        etContent.setSingleLine();
        return this;
    }

    public CustomDialog setPositiveButton(String text, final OnConfirmClickListener listener) {
        showPosBtn = true;
        if (TextUtils.isEmpty(text)) {
            btn_pos.setText("确定");
        } else {
            btn_pos.setText(text);
        }
        btn_pos.setOnClickListener(v -> {
            if (listener != null) {
                listener.onConfirm(etContent.getText().toString());
            }
            dialog.dismiss();
        });
        return this;
    }

    public CustomDialog setPositiveButton(String text, final OnClickListener listener) {
        showPosBtn = true;
        if (TextUtils.isEmpty(text)) {
            btn_pos.setText("确定");
        } else {
            btn_pos.setText(text);
        }
        btn_pos.setOnClickListener(v -> {
            listener.onClick(v);
            dialog.dismiss();
        });
        return this;
    }

    public CustomDialog setPositiveButton(
            final OnConfirmClickListener listener) {
        return this.setPositiveButton(null, listener);
    }

    public CustomDialog setPositiveButton(
            final OnClickListener listener) {
        return this.setPositiveButton(null, listener);
    }

    public CustomDialog setNegativeButton(String text,
                                          final OnClickListener listener) {
        showNegBtn = true;
        if (TextUtils.isEmpty(text)) {
            btn_neg.setText("取消");
        } else {
            btn_neg.setText(text);
        }
        btn_neg.setOnClickListener(v -> {
            listener.onClick(v);
            dialog.dismiss();
        });
        return this;
    }

    public CustomDialog setNegativeButton(final OnClickListener listener) {
        return setNegativeButton(null, listener);
    }

    private void setLayout() {
        if (!showTitle && !showMsg) {
            txt_title.setText("提示");
            txt_title.setVisibility(View.VISIBLE);
        }

        if (showTitle) {
            txt_title.setVisibility(View.VISIBLE);
        }

        if (showMsg) {
            txt_msg.setVisibility(View.VISIBLE);
        }

        if (!showPosBtn && !showNegBtn) {
            btn_pos.setText("确定");
            btn_pos.setVisibility(View.VISIBLE);
            btn_pos.setBackgroundResource(R.drawable.blue_bg_round_btn);
            btn_pos.setOnClickListener(v -> dialog.dismiss());
        }

        if (showPosBtn && showNegBtn) {
            btn_pos.setVisibility(View.VISIBLE);
            btn_pos.setBackgroundResource(R.drawable.blue_bg_round_btn);
            btn_neg.setVisibility(View.VISIBLE);
            btn_neg.setBackgroundResource(R.drawable.blue_border_round_btn);
            img_line.setVisibility(View.VISIBLE);
        }

        if (showPosBtn && !showNegBtn) {
            btn_pos.setVisibility(View.VISIBLE);
            btn_pos.setBackgroundResource(R.drawable.blue_bg_round_btn);
        }

        if (!showPosBtn && showNegBtn) {
            btn_neg.setVisibility(View.VISIBLE);
            btn_neg.setBackgroundResource(R.drawable.blue_border_round_btn);
        }

        if (showNegBtn && TextUtils.isEmpty(btn_neg.getText().toString())) {
            btn_neg.setText("取消");
        }
        if (showPosBtn && TextUtils.isEmpty(btn_pos.getText().toString())) {
            btn_pos.setText("确定");
        }
    }

    public void show() {
        setLayout();
        dialog.show();
    }

    public boolean isShowing() {
        if (dialog != null) {
            return dialog.isShowing();
        } else {
            return false;
        }
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public interface OnCancelClickListener {
        void onCancel();
    }

    public interface OnConfirmClickListener {
        void onConfirm();

        void onConfirm(String etContent);
    }

}